﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iLangl.Transport;
using iLangl.Customer;
using iLangl.Transport.Communication;
using iLangl.BusinessModel;
using iLangl.StateMachine;
using iLangl.LanguageWire;
using iLangl.Plugin.ConfigFileSettings;
using iLangl.BusinessModel.Transport;
using iLangl.Plugin;

namespace iLangl.Tests
{
	public class BaseTest
	{
		protected ITransport clienttransport = new WCFConsumer("http://localhost:8080/ilangl/transport");
		protected ITransport ilangltransport =  new WCFConsumerRouter();
		protected ITransport tptransport = new WCFConsumer("http://localhost:8080/ilangl/transport");

		protected FileSystemWatcher ilanglwatcher;
		protected FileSystemWatcher customerwatcher;
		protected FileSystemWatcher tpwatcher;

		public BaseTest()
		{
			Facade.Instance.Initialize(iLanlFacade.Instance);
			log4net.Config.XmlConfigurator.Configure();

			//File Watcher Setup
			string ilanglIcomingPackagesDirectory = "C:\\FileStorage\\iLangl\\IncomingPackages";
			if (!Directory.Exists(ilanglIcomingPackagesDirectory))
			{
				Directory.CreateDirectory(ilanglIcomingPackagesDirectory);
			}

			string filter = "*.xml";
			ilanglwatcher = new FileSystemWatcher(ilanglIcomingPackagesDirectory);
			ilanglwatcher.Created += new FileSystemEventHandler(ilanglIncomePackage);
			ilanglwatcher.Filter = filter;
			ilanglwatcher.EnableRaisingEvents = true;

			string customerPackagesDirectory = "C:\\FileStorage\\customer\\IncomingPackages";
			if (!Directory.Exists(customerPackagesDirectory))
			{
				Directory.CreateDirectory(customerPackagesDirectory);
			}

			customerwatcher = new FileSystemWatcher(customerPackagesDirectory);
			customerwatcher.Created += new FileSystemEventHandler(customerIncomepackage);
			customerwatcher.Filter = filter;
			customerwatcher.EnableRaisingEvents = true;

			string tpIncomingPackages = "C:\\FileStorage\\tp\\IncomingPackages";
			if (!Directory.Exists(tpIncomingPackages))
			{
				Directory.CreateDirectory(tpIncomingPackages);
			}

			tpwatcher = new FileSystemWatcher(tpIncomingPackages);
			tpwatcher.Created += new FileSystemEventHandler(tpIncomePackage);
			tpwatcher.Filter = filter;
			tpwatcher.EnableRaisingEvents = true;

			StateMachineConfiguration.Instance.SetStateMachineFileName(@"C:\FileStorage\Ilangl\iLanglStateMachine.xml");

			//Ilangl Business Model Test Setup
			iLanlFacade.Instance.CommunicationManager.Transport = ilangltransport;
			iLanlFacade.Instance.StateMachine.SetMutlyThreading(false);
		}

		bool isTP = false;
		bool isCustomer = false;

		void tpIncomePackage(object sender, FileSystemEventArgs e)
		{
			SwitchToTPSide();

			IRobot getrobot = new PackagesRobot();
			IRobotGet robotGet = new PackagesRobotGet(getrobot, e.FullPath);
			IRobotSet robotSet = new PackagesRobotExecute(getrobot);

			getrobot.GetOperation = robotGet;
			getrobot.SetOperation = robotSet;

			
			getrobot.Execute();
		}

		protected virtual void ilanglIncomePackage(object sender, FileSystemEventArgs e)
		{
			if (isTP)
				SwitchToILanglSideForTP();
			if(isCustomer)
				SwitchToILanglSideForCustomer();


			IRobot getrobot = new PackagesRobot();
			IRobotGet robotGet = new PackagesRobotGet(getrobot, e.FullPath);
			IRobotSet robotSet = new PackagesRobotExecute(getrobot);

			getrobot.GetOperation = robotGet;
			getrobot.SetOperation = robotSet;


			getrobot.Execute();
		}

		protected virtual void customerIncomepackage(object sender, FileSystemEventArgs e)
		{
			//Setup customer business model
			SwitchToCustomerSide();


			IRobot getrobot = new PackagesRobot();
			IRobotGet robotGet = new PackagesRobotGet(getrobot, e.FullPath);
			IRobotSet robotSet = new PackagesRobotExecute(getrobot);

			getrobot.GetOperation = robotGet;
			getrobot.SetOperation = robotSet;


			getrobot.Execute();
		}

		//Customer Facade
		CustomerFacade customerFacade = null;
		protected CustomerFacade CustomerFacade
		{
			get
			{
				if (customerFacade == null)
				{

					//Customer Business Model Test Setup
					customerFacade = new CustomerFacade();
					
					customerFacade.CommunicationManager.Transport = clienttransport;
					//Share Authenication Manager
					customerFacade.AuthenticationService = iLanlFacade.Instance.AuthenticationService;
					customerFacade.StateMachine.SetMutlyThreading(false);
				}

				return customerFacade;
			}
		}

		//Translation Provider Facade
		TPFacade tpFacade = null;
		protected TPFacade TPFacade
		{
			get
			{
				if (tpFacade == null)
				{

					StateMachineConfiguration.Instance.SetStateMachineFileName(@"C:\FileStorage\tp\StateMachine.xml");

					//Customer Business Model Test Setup
					tpFacade = new TPFacade();
					tpFacade.CommunicationManager.Transport = tptransport;
					//Share Authenication Manager
					tpFacade.AuthenticationService = iLanlFacade.Instance.AuthenticationService;
					
					tpFacade.StateMachine.SetMutlyThreading(false);
				}

				return tpFacade;
			}
		}


		protected void SwitchToTPSide()
		{
			isTP = true;
			ConfigurationFactory.Instance.Initialize(new FileConfigurationFactory());

			PluginConfiguration.Instance.SetPluginLicenseKey(TestSettings.LicenseKeyLW);
			TransportConfiguration.Instance.SetSettingsForTesting
				(@"C:\FileStorage\tp\IncomingPackages\",
				@"C:\FileStorage\tp\OutcomePackages\",
				@"C:\FileStorage\tp\CorruptedPackages");

			Facade.Instance.Initialize(TPFacade);
		}

		protected void SwitchToCustomerSide()
		{
			isCustomer = true;
			PluginConfiguration.Instance.SetPluginLicenseKey(TestSettings.LicenseKeyTestCustomer);
			ConfigurationFactory.Instance.Initialize(new FileConfigurationFactory());

			TransportConfiguration.Instance.SetSettingsForTesting
				(@"C:\FileStorage\customer\IncomingPackages\",
				@"C:\FileStorage\customer\OutcomePackages\",
				@"C:\FileStorage\customer\DisptachedPackages\");

			Facade.Instance.Initialize(CustomerFacade);
		}

		protected void SwitchToILanglSideForCustomer()
		{
			PluginConfiguration.Instance.SetPluginLicenseKey(TestSettings.LicenseKeyTestCustomer);
			

			TransportConfiguration.Instance.SetSettingsForTesting
				(@"C:\FileStorage\ilangl\IncomingPackages\",
				@"C:\FileStorage\ilangl\OutcomePackages\",
				@"C:\FileStorage\ilangl\DisptachedPackages\");

			//Setup ilangl business model
			Facade.Instance.Initialize(iLanlFacade.Instance);
		}

		protected void SwitchToILanglSideForTP()
		{
			PluginConfiguration.Instance.SetPluginLicenseKey(TestSettings.LicenseKeyLW);

			

			TransportConfiguration.Instance.SetSettingsForTesting
				(@"C:\FileStorage\ilangl\IncomingPackages\",
				@"C:\FileStorage\ilangl\OutcomePackages\",
				@"C:\FileStorage\ilangl\DisptachedPackages\");

			//Setup ilangl business model
			Facade.Instance.Initialize(iLanlFacade.Instance);
		}
	}
}
